UTF8_BYTE_ORDER_MARK   [\xEF][\xBB][\xBF]

D           [0-9]
ASCII       [\x0-\xFF]

U1          [a-zA-Z_]
U2          [\xC2-\xDF][\x80-\xBF]
U3          [\xE0][\xA0-\xBF][\x80-\xBF]
U4          [\xE1-\xEC][\x80-\xBF][\x80-\xBF]
U5          [\xED][\x80-\x9F][\x80-\xBF]
U6          [\xEE-\xEF][\x80-\xBF][\x80-\xBF]
U7          [\xF0][\x90-\xBF][\x80-\xBF][\x80-\xBF]
U8          [\xF1-\xF3][\x80-\xBF][\x80-\xBF][\x80-\xBF]
U9          [\xF4][\x80-\x8F][\x80-\xBF][\x80-\xBF]

L           {ASCII}|{U2}|{U3}|{U4}|{U5}|{U6}|{U7}|{U8}|{U9}
U           [\x0-\xFF]|{U2}|{U3}|{U4}|{U5}|{U6}|{U7}|{U8}|{U9} 

A           [a-zA-Z]

ODATE       [0-9]+.[0-9]{1,2}.[0-9]{1,2}[ ]+[^\n\>]+([ ]+[0-9]{1,2}.[0-9]{1,2})?

%{
    
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "utils.h"
#include "task.h"
 
#include "parser.tab.h"

%}

%option noyywrap

%x header
%x body
%x is_text
%x can_keyword
%x skip_space
%%

<INITIAL,body,is_text>
^[*]+         {
                 yylval.number=strlen(yytext);
                 printf("(TASK)");
                 BEGIN(header,can_keyword);
                 return TASK;
              }

<<EOF>>       {
                 printf("(EOF)");
                 return EOFM;
              }

<can_keyword>
[a-zA-Z]+ {
                 yylval.string=yytext;
                 if(is_keyword(yytext, __class_keywords))
                    {
                        BEGIN(header,skip_space);
                        return KEYWORD;
                    }
                 else
                 {
                     printf("(L!)");
                     BEGIN(header);
                     yyless(0);
                 }

                 

              }

<body>
SCHEDULED:    {
                     printf("(SCHED)");
                     return SCHEDULED;
              }

<body>
CLOSED:       {
                     printf("(CLOSED)");
                     return CLOSED;
              }

<body>
CLOCK:       {
                     printf("(CLOCK)");
                     return CLOCK;
              }


<header,is_text,can_keyword,skip_space>
\[[0-9]+\/[0-9]+\]  {
                  yylval.number = str_to_completion(yytext);
                  return CHECKBOX;
              }


<header,is_text,can_keyword,skip_space>
\[#[A-C]\]    {
                     printf("(PRIO)");
                     yylval.leter=yytext[2];
                     return PRIORITY;
              }

<header,is_text,skip_space>
\[x\]         {
                     yylval.number = 1;
                     return CHECKBOX;
              }


<header,is_text,skip_space>
:[^ \t\n]+: {
                     yylval.string = yytext;
                     printf("(TAG)");
                     return TAG;
              }


<header,is_text,skip_space>
:             {
                     yylval.string = yytext;
                     printf("(:)");
                     return TEXT;
              }

<header>
\[\ \]        {
                     yylval.number = 0;
                     return CHECKBOX;
              }

<body>
\[{ODATE}\]--\[{ODATE}\]    {
                     printf("(DATE:RANGE)");
                     /* just ignore for now */
                     return DATERANGE;
              }

<body>
\<{ODATE}\>--\<{ODATE}\>    {
                     printf("(DACT:RANGE)");
                     /* just ignore for now */
                     return DATEACTRANGE;
              }



<body>
\<{ODATE}\>   {
                     printf("(DATE)");
                     yylval.time=str_to_time(yytext,
                                             1, 1);
                     return DATE;
              }

<body>
\[{ODATE}\]    {
                     printf("(DACT)");
                     yylval.time=str_to_time(yytext,
                                             1, 1);
                     return DATEACT;
              }


<body>
\n+           {
                     printf("(\\n)");
                     yylval.string=yytext;
                     return NEWLINE;
              }

<header,is_text,skip_space>
\n+           {
                     printf("(\\n!)");
                     yylval.string=yytext;
                     BEGIN(body);
                     return NEWLINE;
              }


<header,is_text,skip_space,can_keyword>
[^\n*\[\<:\t\ ]+  {
                     yylval.string=yytext;
                     printf("(TEXT@)");
                     BEGIN(header,is_text);
                     return TEXT;
                   }

<body>
[^\n*\[\<\t\ ]+  {
                     yylval.string=yytext;
                     printf("(TEXT=>)");
                     return TEXT;
               }

<header,is_text,body>
[\t\ ]+         {
                     yylval.string=yytext;
                     printf("(TEXT:DM!)");
                     return TEXT;

                }

%%

